package com.gitee.wsl.doc.prosemirror.model.content

import com.gitee.wsl.doc.prosemirror.exception.RangeError
import com.gitee.wsl.doc.prosemirror.model.node.Node

interface DocStruct {

    val children: List<Node>

    // The number of children that the node has.
    val childCount: Int
        get() = children.size

    // The first child of the fragment, or `null` if it is empty.
    val firstChild: Node?
        get() = children.firstOrNull()

    // The last child of the fragment, or `null` if it is empty.
    val lastChild: Node?
        get() = children.lastOrNull()

    // Get the child node at the given index. Raise an error when the index is out of range.
    fun child(index: Int): Node {
        return children.getOrNull(index) ?: throw RangeError("Index $index out of range for $this")
    }

    // Get the child node at the given index, if it exists.
    fun maybeChild(index: Int): Node? {
        return children.getOrNull(index)
    }

    // Call `f` for every child node, passing the node, its offset into this parent node, and its index.
    fun forEach(f: (node: Node, offset: Int, index: Int) -> Unit)

}